package zl.osimall.front.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import zl.osimall.common.bean.*;
import zl.osimall.common.exception.SteduException;
import zl.osimall.common.service.AddrService;
import zl.osimall.common.service.CartService;
import zl.osimall.common.service.OrderService;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user/order")
public class OrderController {
    @Autowired
    private AddrService addrService;
    @Autowired
    private CartService cartService;
    @Autowired
    private OrderService orderService;

    @PostMapping("/confirm")
    public String confirm(Integer[] cartId, HttpSession session, Model m){
        try {
            User user = (User) session.getAttribute("user");
            Integer userId = user.getId();
            List<Addr> addrList = addrService.selectByUserId(userId);
            List<Cart> cartList = cartService.selectByIds(cartId, userId);
            cartList.stream().forEach(System.out::println);
            m.addAttribute("addrList",addrList);
            m.addAttribute("cartList", cartList);
            return "user/orderConfirm";
        } catch (SteduException e) {
            e.printStackTrace();
            m.addAttribute("errMsg", e.getMessage());
            return "user/error";
        }
    }

    @GetMapping("/add")
    public String add(Integer[] cartId, Integer addressId, HttpSession session, Model m ){
        try {
            User user = (User) session.getAttribute("user");
            Integer userId = user.getId();
            Order order = new Order();
            order.setUserId(userId);
            order.setAddrId(addressId);
            orderService.add(order, cartId);
            return "redirect:/user/order/toPay?orderId=" + order.getId();
        }catch (SteduException e){
            e.printStackTrace();;
            m.addAttribute("errMsg", e.getMessage());
            return "user/error";
        }
    }

    @GetMapping("/toPay")
    public String toPay(String orderId, Model m){
        try {
            Order order = orderService.selectById(orderId);
            m.addAttribute("order", order);
            return "user/pay";
        } catch (SteduException e) {
            e.printStackTrace();;
            m.addAttribute("errMsg", e.getMessage());
            return "user/error";
        }
    }


    @RequestMapping("/pay")
    public String pay(String orderId, String paypwd, HttpSession session, Model m){
        try {
            User user = (User) session.getAttribute("user");
            Integer userId = user.getId();
            orderService.pay(orderId, userId, paypwd);
            return "redirect:/user/order/list";
        }  catch (SteduException e){
            e.printStackTrace();
            m.addAttribute("errMsg", e.getMessage());
            return "user/error";
        }
    }




    @RequestMapping("/list")
    public String list(Integer status,Model m){
        if (status == null){
            status = 7;
        }
        m.addAttribute("status", status);
        return "user/orderList";
    }

    @ResponseBody
    @RequestMapping("/getList")
    public RespBean getList(Integer pageNum,Integer status, HttpSession session){
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        Order condition = new Order();
        condition.setUserId(userId);
        if (status == null){
            status = 7;
        }
        if (status!=7){
            condition.setStatus(status);
        }
        /*List<Order> orderList = orderService.selectByUserId(userId);*/
        PageInfo<Order> orderList = orderService.selectByPage(condition ,pageNum, 1);
        return RespBean.ok("",orderList);
    }

    @ResponseBody
    @RequestMapping("/update")
    public RespBean update(String id ,Integer status, HttpSession session){
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        Order condition = new Order();
        condition.setId(id);
        condition.setUserId(userId);
        condition.setStatus(status);
        try {
            Boolean i =orderService.update(condition);
            if(!i){
                return RespBean.error("修改失败");
            }
        } catch (SteduException e) {
            throw new RuntimeException(e);
        }
        return RespBean.ok("修改成功");
    }
}
